//
//  ViewController.m
//  10802pickerView
//
//  Created by student on 16/2/23.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;

@property (weak, nonatomic) IBOutlet UILabel *label;

@property (strong,nonatomic) NSArray *leftArr;
@property (weak, nonatomic) IBOutlet UIButton *displayBtn;

@property (strong,nonatomic) NSArray *rightArr;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    
    self.leftArr = @[@"清蒸",@"红烧",@"油炸",@"爆炒"];
    self.rightArr = @[@"鸡",@"鸭",@"鱼",@"肉",@"青菜",@"萝卜",@"黄瓜"];
    NSString *str = [NSString stringWithFormat:@"今天想吃%@的%@",self.leftArr[arc4random()%4],self.rightArr[arc4random()%7]];
    [self.label setText:str];
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return self.leftArr.count;
    }
    else{
        return self.rightArr.count;
    }
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) {
        return self.leftArr[row];
    }
    else{
        return self.rightArr[row];
    }
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    long sel1 = [self.pickerView selectedRowInComponent:0];
    long sel2 = [self.pickerView selectedRowInComponent:1];
    NSString *str = [NSString stringWithFormat:@"你的伙食是%@的%@",self.leftArr[sel1],self.rightArr[sel2]];
    [self.displayBtn setTitle:str forState:UIControlStateNormal];
}


- (IBAction)didClickBtn {
    long sel1 = [self.pickerView selectedRowInComponent:0];
    long sel2 = [self.pickerView selectedRowInComponent:1];
    NSString *str = [NSString stringWithFormat:@"你选择了%@的%@",self.leftArr[sel1],self.rightArr[sel2]];
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:str message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alert addAction:action1];
    [alert addAction:action2];
    [self presentViewController:alert animated:YES completion:nil];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
